<template>
  <el-form-item :label="label" :size="size">
    <el-radio-group v-model="leaveDuration" :size="size">
      <el-radio-button v-for="option in durationOptions" :key="option.value" :label="option.label" :value="option.value" />
    </el-radio-group>
  </el-form-item>
  <LeaveApplyDate :size="size" v-model="value1" :leaveDuration="leaveDuration" :leave-category="leaveCategory" :defaultDisabledDateStart="disabledDateStart" />

  <el-alert v-if="alertFlag" title="本月一小时已用完" type="error" />
</template>
<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import LeaveApplyDate from './LeaveApplyDate.vue'
import { overtimeStatusEnum } from '../../../definitions/status'
import { useUsersStore } from '../../../stores'
import { formatDate } from '../../../utils'
import api from '../../../api'

const emit = defineEmits(['update:modelValue'])
const props = defineProps({
  label: {
    type: String,
    default: '时间段范围',
  },
  size: {
    type: String,
    default: 'default',
  },
  modelValue: {
    type: Object as any,
  },
  leaveCategory: {
    type: String,
    default: '',
  },
})

const value1 = ref()
const alertFlag = ref<any>(false)
const currentUser = useUsersStore().getAllState()
const leaveDuration = ref(3)

// 请假时长类型
const durationOptions = computed(() => {
  const list = [
    { label: '半天', value: 2, disabled: false },
    { label: '全天', value: 3, disabled: false },
    { label: '多天', value: 4, disabled: false },
  ]

  if (props.leaveCategory === '1' || props.leaveCategory === '2') {
    let label = '小时'
    if ((useUsersStore().user as any).city == '长春' && props.leaveCategory === '2') {
      label = '一小时'
    }
    list.unshift({ label, value: 1, disabled: false })
  }
  if ((useUsersStore().user as any).city == '北京' && props.leaveCategory === '3') {
    list.unshift({ label: '小时', value: 1, disabled: false })
  }
  return list
})

const getLeaveList = async () => {
  // console.log("(props.leaveCategory === '1' ", props.leaveCategory === '1')
  // console.log('value1.value && value1.value.startTime', value1)

  let len = false
  // 调休，事假，
  if (props.leaveCategory === '2' && value1.value && value1.value.startTime) {
    console.log('res', value1.value)
    const currentMonth = value1.value.startTime
    const start = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), 1)
    const end = new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 0)
    const params = {
      pageSize: 99999,
      leaveCategory: props.leaveCategory,
      leaveStatus: overtimeStatusEnum.总经理通过,
      userName: currentUser.username,
      startTime: formatDate(start, 'yyyy-MM-dd'),
      endTime: formatDate(end, 'yyyy-MM-dd'),
    }
    const res = await api.Leave.getList(params)
    len = res.length > 0
  }
  return len
}

/**
 * 判断日期是否在当前日期之前
 *
 * @param time 要判断的日期
 * @returns 如果 time 在当前日期之前，返回 true；否则返回 false
 */
const disabledDateStart = (_: Date) => {
  // const startOfDay = new Date()
  // startOfDay.setHours(0, 0, 0, 0)
  // const startOfDayTimestamp = startOfDay.getTime()

  // return time.getTime() < startOfDayTimestamp
  return false
}

watch([() => value1.value, () => leaveDuration.value], async () => {
  // console.log('watch', leaveDuration.value)
  if (leaveDuration.value == 1) {
    alertFlag.value = await getLeaveList()
    // console.log('alertFlag', alertFlag.value)
    // const falg = getLeaveList()
    // watch:leaveDuration.value
    // 如果 leaveDuration.value = 1, 检查当前月是否有可用的请假
    // 如果没有，当前月的第一天到最后一天，禁用
    // 如果有，不禁用

    // watch:value1.value
    // 如果 leaveDuration.value = 1, 检查当前月是否有可用的请假
    // 如果没有，显示提示，并禁用 “一小时”
    // 如果有，不禁用
  }
  emit('update:modelValue', { ...value1.value, leaveDuration: leaveDuration.value })
})
</script>
